package com.qingke.qkmall.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingke.qkmall.pojo.Shop;
import com.qingke.qkmall.pojo.ShopQueryVo;
import com.qingke.qkmall.serviceInterface.ShopService;

/**
 * 商品Controller
 * @author Administrator
 *
 */
@Controller
public class ShopController {
	
	@Autowired
	private ShopService shopService;
	
	@RequestMapping(value = "/shoplist") 
	public String selectAllShop(Model model) {
		List<ShopQueryVo> allShop = shopService.selectAllShop(null);
		model.addAttribute("allShop", allShop);
		return "shopList";
	}
	
	@RequestMapping(value = "/criteriaShoplist")
	@ResponseBody
	public List<ShopQueryVo> criteriaShop(int categoryId) {
		List<ShopQueryVo> allShop = shopService.selectAllShop(categoryId);
		return allShop;
	}
	
	@RequestMapping(value = "/shopEdit")
	public String selectShopById(int shopId, Model model) {
		ShopQueryVo sqv = shopService.selectShopById(shopId);
		model.addAttribute("shop", sqv);
		return "shopEdit";
	}
	
	@RequestMapping(value = "/shopAdd")
	public String insertShop(ShopQueryVo sqv) {
		int result = shopService.insertShop(sqv);
		if(result == 1) {
			return "redirect:/shoplist";
		} else {
			return "error";
		}
	}
	
	//验证名字是否存在
	@RequestMapping(value = "/shopNameVaildate")
	@ResponseBody
	public Shop selectShopByName(String shopName) {
		Shop result = shopService.selectShopByName(shopName);
		return result;
	}
	
	@RequestMapping(value = "/readyShopAdd")
	public String addShop() {
		return "shopAdd";
	}
}
